package org.hikari.qqbot.xiuxian.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hikari.qqbot.xiuxian.command.ExecutableCommand;
import org.hikari.qqbot.xiuxian.service.impl.DefaultTaskService;
import org.hikari.qqbot.xiuxian.task.UserTaskRuntime;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;
import java.util.stream.Collectors;

/**
 * created at 2024-12-20 09:37:58
 *
 * @author yinchao
 * @version 1.0
 **/
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/debug")
public class DebugController {

    private final DefaultTaskService defaultTaskService;

    @GetMapping("/{userId}/queue")
    public Object queue(@PathVariable Long userId) {
        UserTaskRuntime runtime = defaultTaskService.getUserTaskRuntime(userId);
        if (runtime == null) {
            return "用户id不存在";
        }
        return runtime.getRunner().searchCommand(c -> true)
                .map(ExecutableCommand::toString)
                .collect(Collectors.joining("\n"));
    }

    @GetMapping("/{userId}/queue/{taskId}")
    public Object queue(@PathVariable Long userId, @PathVariable String taskId) {
        UserTaskRuntime runtime = defaultTaskService.getUserTaskRuntime(userId);
        if (runtime == null) {
            return "用户id不存在";
        }
        return runtime.getRunner().searchCommand(c -> Objects.equals(c.getTaskId(), taskId))
                .map(ExecutableCommand::toString)
                .collect(Collectors.joining("\n"));
    }

//    @GetMapping("setting/user/{userId}/")
//    public Object userSetting(@PathVariable Long userId) {
//        UserTaskRuntime runtime = defaultTaskService.getUserTaskRuntime(userId);
//        if (runtime == null) {
//            return "用户id不存在";
//        }
//        return runtime.getUserSetting();
//    }
//
//    @GetMapping("setting/task/{taskId}/")
//    public Object userSetting(@PathVariable String taskId) {
//        UserTaskRuntime runtime = defaultTaskService.getUserTaskRuntime(userId);
//        if (runtime == null) {
//            return "用户id不存在";
//        }
//        return runtime.getUserSetting();
//    }

    @GetMapping("/{userId}/meta")
    public Object meta(@PathVariable Long userId) {
        UserTaskRuntime runtime = defaultTaskService.getUserTaskRuntime(userId);
        if (runtime == null) {
            return "用户id不存在";
        }
        return runtime.getMeta();
    }

    @GetMapping("/{userId}/meta/last-command")
    public Object lastCommandInfo(@PathVariable Long userId) {
        UserTaskRuntime runtime = defaultTaskService.getUserTaskRuntime(userId);
        if (runtime == null) {
            return "用户id不存在";
        }
        return runtime.getMeta().getLastCommandInfo();
    }

    @GetMapping("/{userId}/meta/last-command/{taskId}")
    public Object taskLastCommandInfo(@PathVariable Long userId, @PathVariable String taskId) {
        UserTaskRuntime runtime = defaultTaskService.getUserTaskRuntime(userId);
        if (runtime == null) {
            return "用户id不存在";
        }
        return runtime.getMeta().getTaskLastCommandInfo(taskId);
    }

}
